<?php
if(!defined('FROM_INDEX'))exit('Invalid request');

require_once ROOT_PATH.'include/sql/class_db.php';
require_once ROOT_PATH.'include/sql/sql_queries.php';

/**
 * 
 * Get user info from DB
 * 
 * @param string $id: User name, or user id
 * 
 * @return array $user
 */
function get_user($id)
{
	$key = md5("get_user_$id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_user_sql();
	
	$results = DBFunction::getInstance()->getRow($sql, array($id, $id));
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function get_user_group_list()
{
	$key = md5("get_user_group_list");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_user_group_list_sql();
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		cache_set($key, $results);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_user($request)
{
	$time = time();

	$params = array(
		$request['username'],
		$request['username'],
		$request['email'],
		$request['password'],
		'',
		$request['group_id'],
		$request['is_admin'],
		$time,
		$request['ip'],
		$request['ip'],
		$time
	);
	
	$sql = add_user_sql();
	
	$results = DBFunction::getInstance()->insert($sql, $params);
	
	return ($results !== false) ? true : false;
}

function delete_user($ids)
{	
	$sql = delete_user_sql($ids);
	$result = DBFunction::getInstance()->execute($sql);

	return ($result !== false) ? true : false;
}

function get_movie_category_list()
{
	$key = md5("get_movie_category_list");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_category_list_sql();
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE);
		return $results;
	}
	else
	{
		return array();
	}
}

function update_movie_category($request)
{
	$key = md5("get_movie_category_list");
	
	$params = array(
		$request['name'],
		$request['order'],
		$request['id'],
	);
	
	$sql = update_movie_category_sql();
	
	$results = DBFunction::getInstance()->execute($sql, $params);
	
	cache_delete($key);
	
	return ($results !== false) ? true : false;
}

function add_movie_category($request)
{
	$key = md5("get_movie_category_list");
	
	$time = time();

	$params = array(
		$request['name'],
		$request['order']
	);
	
	$sql = add_movie_category_sql();
	
	$results = DBFunction::getInstance()->insert($sql, $params);
	
	cache_delete($key);
	
	return ($results !== false) ? true : false;
}

/**
 * 
 * Get movie list by category id
 * 
 * @param array $request
 * 
 * @return array $results
 */
function get_movie_list($request)
{
	$time = time();
	
	$from_id = $request['from_id'];
	$to_id = $request['to_id'];
	$page_num = $request['page_num'];
	$page_limit = $request['page_limit'];
	
	$category_id = (isset($request['category_id']) && $request['category_id']) ? $request['category_id'] : '';
	$is_hot = (isset($request['is_hot']) && $request['is_hot']) ? $request['is_hot'] : '';
	$is_expect = (isset($request['is_expect']) && $request['is_expect']) ? $request['is_expect'] : '';
	
	$key = md5("get_movie_list $from_id $to_id $page_num $page_limit $category_id $is_hot");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$page_num *= $page_limit;
	$limit = " limit $page_num, $page_limit";
	
	$where = " where `is_delete` = '0'";
	
	if($category_id)
	{
		$where .= " and `category_id` = '$category_id'";
	}
	if($is_hot)
	{
		//TODO, for test
		//$where .= " and (`is_hot` = '1' and `release_date` < '$time')";
		$where .= " and (`is_hot` = '1')";
	}
	if($is_expect)
	{
		//TODO, for test
		//$where .= " and (`is_expect` = '1' and `release_date` > '$time')";
		$where .= " and (`is_expect` = '1')";
	}
	if($from_id)
	{
		$where .= " and `id` > '$from_id'";
	}
	if($to_id)
	{
		$where .= " and `id` < '$to_id'";
	}
	
	$sql = get_movie_list_sql();
	$sql .= $where.' order by `release_date` desc '.$limit;
	
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		foreach($results as &$item)
		{
			$item['actors'] = get_movie_actor_list($item['id']);
			
			$images = get_movie_images($item['id'], BASE_URL, '1');
			$item['image_list'] = $images;
		}
		
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE);
		return $results;
	}
	else
	{
		return array();
	}
}

function get_movie_info($movie_id)
{	
	$key = md5("get_movie_info_$movie_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_info_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($movie_id));
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE);
		return $results;
	}
	else
	{
		return array();
	}
}

function check_movie_exists($name, $id='')
{	
	$sql = check_movie_exists_sql();
	$results = DBFunction::getInstance()->getOne($sql, array($name, $id));
	
	return ($results) ? true : false;
}

/**
 * 
 * Get movie description by movie id
 * 
 * @param string $movie_id
 * 
 * @return array $results
 */
function get_movie_desc($movie_id)
{
	$key = md5("get_movie_desc_$movie_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_desc_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($movie_id));
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE);
		return $results;
	}
	else
	{
		return array();
	}
}

/**
 * 
 * Get movie actor list
 * 
 * @param string $movie_id
 * 
 * @return array $results
 */
function get_movie_actor_list($movie_id)
{
	$key = md5("get_movie_actor_list_$movie_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_actor_list_sql();
	$results = DBFunction::getInstance()->getAll($sql, array($movie_id));
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_movie($request)
{
	$time = time();

	$params = array(
		$request['name'],
		'',
		$request['director'],
		$request['release_date'],
		$request['star'],
		$request['category_id'],
		$time,
		$time,
		$request['is_hot'],
		$request['is_expect']
	);
	
	$sql = add_movie_sql();
	
	$movie_id = DBFunction::getInstance()->insert($sql, $params);
	
	if(is_numeric($movie_id) && $movie_id > 0)
	{
		$sql = add_movie_desc_sql();
		$result = DBFunction::getInstance()->insert($sql, array($movie_id, $request['desc'], $time, $time));
		if(!is_numeric($result))
		{
			return false;
		}
		
		if(check_array($request['actor']))
		{
			$sql = add_movie_actors_sql();
			foreach($request['actor'] as $act_name)
			{
				DBFunction::getInstance()->insert($sql, array($movie_id, $act_name, '', '0', '1'));
			}
		}
		
	}
	
	//TODO, clear cache
	cache_delete(md5("get_movie_list   0 20 {$request['category_id']} {$request['is_hot']}"));
	
	return $movie_id;
}

function update_movie($request)
{
	$time = time();

	$params = array(
		$request['name'],
		'',
		$request['director'],
		$request['release_date'],
		$request['star'],
		$request['category_id'],
		$time,
		$request['is_hot'],
		$request['is_expect'],
		$request['id']
	);
	
	$sql = update_movie_sql();
	
	$result = DBFunction::getInstance()->update($sql, $params);
	
	if($result !== false)
	{
		$sql = update_movie_desc_sql();
		$result = DBFunction::getInstance()->update($sql, array($request['desc'], $time, $request['id']));
		
		if(check_array($request['actor']))
		{
			delete_movie_actors($request['id']);
			
			$sql = add_movie_actors_sql();
			foreach($request['actor'] as $act_name)
			{
				DBFunction::getInstance()->insert($sql, array($request['id'], $act_name, '', '0', '1'));
			}
		}
		
	}
	
	return true;
}

function delete_movie_actors($movie_id)
{
	$sql = delete_movie_actors_sql();
	$result = DBFunction::getInstance()->execute($sql, $movie_id);
	return ($result !== false) ? true : false;
}

function delete_movie($ids)
{
	$time = time();
	
	$sql = delete_movie_sql($ids);
	$result1 = DBFunction::getInstance()->execute($sql);
	
	$sql = delete_movie_to_trash_sql($ids);
	$result2 = DBFunction::getInstance()->execute($sql, array($time));

	return ($result1 !== false && $result2 !== false) ? true : false;
}

function delete_movie_desc($ids)
{
	$sql = delete_movie_desc_sql($ids);
	$result = DBFunction::getInstance()->execute($sql);
	return ($result !== false) ? true : false;
}

function get_trash_movie_ids($ids)
{
	$sql = get_trash_movie_ids_sql($ids);
	$results = DBFunction::getInstance()->getAll($sql);
	
	return check_array($results) ? $results : array();
}

function get_movie_image_ids($movie_ids)
{
	$sql = get_movie_image_ids_sql($movie_ids);
	$results = DBFunction::getInstance()->getAll($sql);
	
	return check_array($results) ? $results : array();
}

/**
 * 
 * Get movie image list
 * 
 * @param string $movie_id
 * @param string $category_id: 1-影片缩略图, 2-剧照
 * 
 * @return array $results
 */
function get_movie_image_list($movie_id, $category_id)
{
	$key = md5("get_movie_image_list_$movie_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_image_list_sql();
	$results = DBFunction::getInstance()->getAll($sql, array($movie_id, $category_id));
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE);
		return $results;
	}
	else
	{
		return array();
	}
}

/**
 * 
 * Get movie refer list by movie id
 * 
 * @param array $request
 * 
 * @return array $results
 */
function get_movie_refer_list($request)
{
	$from_id = (isset($request['from_id']) && $request['from_id']) ? $request['from_id'] : '';
	$to_id = (isset($request['to_id']) && $request['to_id']) ? $request['to_id'] : '';
	$page_num = (isset($request['page_num']) && $request['page_num']) ? $request['page_num'] : 0;
	$page_limit = (isset($request['page_limit']) && $request['page_limit']) ? $request['page_limit'] : DEFAULT_PER_PAGE;
	
	$movie_id = (isset($request['movie_id']) && $request['movie_id']) ? $request['movie_id'] : '';

	$key = md5("get_movie_refer_list $from_id $to_id $page_num $page_limit $movie_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$page_num *= $page_limit;
	$limit = " limit $page_num, $page_limit";
	
	$where = " where `movie_id` = '{$movie_id}'";
	
	$sql = get_movie_refer_list_sql();
	$sql .= $where.' '.$limit;
	
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE);
		return $results;
	}
	else
	{
		return array();
	}
}

/**
 * 
 * Get movie titbit list by movie id
 * 
 * @param array $request
 * 
 * @return array $results
 */
function get_movie_titbit_list($request)
{
	$from_id = (isset($request['from_id']) && $request['from_id']) ? $request['from_id'] : '';
	$to_id = (isset($request['to_id']) && $request['to_id']) ? $request['to_id'] : '';
	$page_num = (isset($request['page_num']) && $request['page_num']) ? $request['page_num'] : 0;
	$page_limit = (isset($request['page_limit']) && $request['page_limit']) ? $request['page_limit'] : DEFAULT_PER_PAGE;
	
	$movie_id = (isset($request['movie_id']) && $request['movie_id']) ? $request['movie_id'] : '';
	
	$key = md5("get_movie_titbit_list $from_id $to_id $page_num $page_limit $movie_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$page_num *= $page_limit;
	$limit = " limit $page_num, $page_limit";
	
	$where = " where `movie_id` = '{$movie_id}'";
	
	$sql = get_movie_titbit_list_sql();
	$sql .= $where.' '.$limit;
	
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE);
		return $results;
	}
	else
	{
		return array();
	}
}

function update_movie_status($field, $ids, $type='')
{
	$time = time();
	
	$status = ($type == '') ? '1' : '0';
	
	$sql = update_movie_status_sql($field, $ids, $status);
	
	$results = DBFunction::getInstance()->update($sql);
	
	return ($results !== false) ? true : false;
}

/**
 * 
 * Get movie review list by movie id
 * 
 * @param array $request
 * 
 * @return array $results
 */
function get_movie_review_list($request)
{
	$from_id = (isset($request['from_id']) && $request['from_id']) ? $request['from_id'] : '';
	$to_id = (isset($request['to_id']) && $request['to_id']) ? $request['to_id'] : '';
	$page_num = (isset($request['page_num']) && $request['page_num']) ? $request['page_num'] : 0;
	$page_limit = (isset($request['page_limit']) && $request['page_limit']) ? $request['page_limit'] : DEFAULT_PER_PAGE;
	
	$movie_id = (isset($request['movie_id']) && $request['movie_id']) ? $request['movie_id'] : '';
	
	$key = md5("get_movie_review_list $from_id $to_id $page_num $page_limit $movie_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$page_num *= $page_limit;
	$limit = " limit $page_num, $page_limit";
	
	$where = " where `movie_id` = '{$movie_id}'";
	if($from_id)
	{
		$where .= " and `id` > '$from_id'";
	}
	if($to_id)
	{
		$where .= " and `id` < '$to_id'";
	}
	
	$sql = get_movie_review_list_sql();
	$sql .= $where.' order by `reply_count` desc '.$limit;
	
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		foreach($results as &$item)
		{
			$images = get_movie_images($item['id'], BASE_URL, '3');
			$item['image_list'] = check_array($images) ? $images : get_face_random(BASE_URL, 1);
		}

		cache_set($key, $results, CACHE_TIMEOUT_MOVIE_REVIEW);
		
		return $results;
	}
	else
	{
		return array();
	}
}

/**
 * 
 * Get movie review detail by review id
 * 
 * @param string $review_id
 * 
 * @return array $results
 */
function get_movie_review_detail($review_id)
{
	$key = md5("get_movie_review_detail_$review_id");
	
	// for clear the movie review cache after add reply or add vote 
	cache_set('movie_review_keys', $key);
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_review_detail_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($review_id));
	
	if(check_array($results))
	{
		reset_movie_review_reply_count($review_id);
				
		$images = get_movie_images($results['id'], BASE_URL, '3');
		$results['image_list'] = check_array($images) ? $images : get_face_random(BASE_URL, 1);
			
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE_REVIEW); // cache one hour
		
		return $results;
	}
	else
	{
		return array();
	}
}

/**
 * 
 * Get movie review reply list by review id
 * 
 * @param array $request
 * 
 * @return array $results
 */
function get_movie_review_reply_list($request)
{
	$from_id = (isset($request['from_id']) && $request['from_id']) ? $request['from_id'] : '';
	$to_id = (isset($request['to_id']) && $request['to_id']) ? $request['to_id'] : '';
	$page_num = (isset($request['page_num']) && $request['page_num']) ? $request['page_num'] : 0;
	$page_limit = (isset($request['page_limit']) && $request['page_limit']) ? $request['page_limit'] : DEFAULT_PER_PAGE;
	
	$review_id = (isset($request['review_id']) && $request['review_id']) ? $request['review_id'] : '';
	
	$key = md5("get_movie_review_reply_list $from_id $to_id $page_num $page_limit $review_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$page_num *= $page_limit;
	$limit = " limit $page_num, $page_limit";
	
	$where = " where `movie_review_id` = '{$review_id}'";
	if($from_id)
	{
		$where .= " and `id` > '$from_id'";
	}
	if($to_id)
	{
		$where .= " and `id` < '$to_id'";
	}
	
	$sql = get_movie_review_reply_list_sql();
	$sql .= $where.' order by `id` desc '.$limit;
		
	$results = DBFunction::getInstance()->getAll($sql);
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE_REVIEW); // cache 5 mins
		return $results;
	}
	else
	{
		return array();
	}
}

function add_movie_review_reply_count($review_id)
{
	$sql = add_movie_review_reply_count_sql();
	$results = DBFunction::getInstance()->update($sql, array($review_id));
	return ($results !== false) ? true : false;
}

/**
 * 
 * Add movie review reply
 * 
 * @param array $request
 * 
 * @return true | false
 */
function add_movie_review_reply($request)
{
	$time = time();

	$sql = add_movie_review_reply_sql();
	
	$params = array($request['movie_id'], $request['review_id'], $request['user_id'], $request['user_addr'], $request['content'], 0, 0, $time, $time);
	
	$results = DBFunction::getInstance()->insert($sql, $params);
	
	if(!$results)
	{
		return false;
	}
	
	add_movie_review_reply_count($request['review_id']);
	
	//TODO, clear cache
	//cache_delete(md5("get_movie_review_reply_list $from_id $to_id $page_num $page_limit ".$request['review_id']));
	cache_delete(md5("get_movie_review_detail_".$request['review_id']));
	
	return true;
}

/**
 * 
 * Add movie review recommend
 * 
 * @param array $request
 * 
 * @return true | false
 */
function add_movie_review_recommend_count($request)
{
	$time = time();
	
	$sql = add_movie_review_recommend_count_sql();
	
	$results = DBFunction::getInstance()->update($sql, array($request['review_id']));
	
	if($results === false)
	{
		return false;
	}
	
	//TODO, clear cache
	//cache_delete(md5("get_movie_review_reply_list $from_id $to_id $page_num $page_limit ".$request['review_id']));
	cache_delete(md5("get_movie_review_detail_".$request['review_id']));
	
	return true;	
}

function get_movie_review_reply($reply_id)
{
	$key = md5("get_movie_review_reply_$reply_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_review_reply_sql();
	
	$results = DBFunction::getInstance()->getRow($sql, array($reply_id));
	
	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE_REVIEW);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_movie_review_reply_vote($request)
{
	
	$set = '';
	if($request['vote'] == 'up')
	{
		$set = 'up_count = up_count + 1';
	}
	else
	{
		$set = 'down_count = down_count + 1';
	}
	
	$sql = add_movie_review_reply_vote_sql($set);
	
	$results = DBFunction::getInstance()->update($sql, array($request['reply_id']));
	
	if($results === false)
	{
		return false;
	}
	
	//TODO, clear cache
	
	return true;	
}

function get_movie_review_info($movie_review_id)
{
	$key = md5("get_movie_review_info_$movie_review_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_review_info_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($movie_review_id));

	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE_REVIEW); // cache 5 mins
		return $results;
	}
	else
	{
		return array();
	}
}

function check_movie_review_exists($title, $mid, $id='')
{
	$sql = check_movie_review_exists_sql();
	$results = DBFunction::getInstance()->getOne($sql, array($title, $mid, $id));
	
	return $results ? true : false;
}

function add_movie_review($request)
{
	$time = time();
	
	$params = array(
				$request['movie_id'],
				$request['title'],
				$request['author'],
				$request['short_desc'],
				0,
				0,
				0,
				0,
				$request['star'],
				$time,
				$time);
	
	$sql = add_movie_review_sql();
	
	$results = DBFunction::getInstance()->insert($sql, $params);
	
	if($results === false)
	{
		return false;
	}
	else
	{
		$sql = add_movie_review_desc_sql();
		$results = DBFunction::getInstance()->insert($sql, array($results, $request['content'], $time, $time));
	}
	
	return ($results !== false) ? $results : false;
}

function update_movie_review($request)
{
	$time = time();
	
	$params = array(
				$request['title'],
				$request['author'],
				$request['star'],
				$request['short_desc'],
				$time,
				$request['id']);
	
	$sql = update_movie_review_sql();
	
	$results = DBFunction::getInstance()->update($sql, $params);
	
	if($results === false)
	{
		return false;
	}
	else
	{
		$sql = update_movie_review_desc_sql();
		$results = DBFunction::getInstance()->insert($sql, array($request['content'], $time, $request['id']));
	}
	
	return ($results !== false) ? true : false;
}

function delete_movie_review($ids)
{
	$time = time();
	
	$sql = delete_movie_review_sql($ids);
	$result1 = DBFunction::getInstance()->execute($sql);
	
	$sql = delete_movie_review_to_trash_sql($ids);
	$result2 = DBFunction::getInstance()->execute($sql, array($time));

	return ($result1 !== false && $result2 !== false) ? true : false;
}

function delete_movie_review_desc($review_ids)
{
	$sql = delete_movie_review_desc_sql($review_ids);
	$result = DBFunction::getInstance()->execute($sql);

	return ($result !== false) ? true : false;
}

function recover_movie_review($ids)
{
	$time = time();
	
	$sql = recover_movie_review_sql($ids);
	$result = DBFunction::getInstance()->update($sql, array($time));

	return ($result !== false) ? true : false;
}

function get_movie_review_ids($movie_ids)
{
	$sql = get_movie_review_ids_sql($movie_ids);
	$results = DBFunction::getInstance()->getAll($sql);
	
	return check_array($results) ? $results : array();
}

function get_trash_review_ids($review_ids)
{
	$sql = get_trash_review_ids_sql($review_ids);
	$results = DBFunction::getInstance()->getAll($sql);
	
	return check_array($results) ? $results : array();	
}

function get_trash_review_reply_ids($review_ids)
{
	$sql = get_trash_review_reply_ids_sql($review_ids);
	$results = DBFunction::getInstance()->getAll($sql);
	
	return check_array($results) ? $results : array();	
}

function get_movie_review_reply_info($review_reply_id)
{
	$key = md5("get_movie_review_reply_info_$review_reply_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_review_reply_info_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($review_reply_id));

	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT_MOVIE_REVIEW); // cache 5 mins
		return $results;
	}
	else
	{
		return array();
	}
}

function update_movie_review_reply($request)
{
	$time = time();
	
	$params = array(
				$request['content'],
				$time,
				$request['id']);
	
	$sql = update_movie_review_reply_sql();
	
	$results = DBFunction::getInstance()->update($sql, $params);
	
	return ($results !== false) ? true : false;
}

function reset_movie_review_reply_count($review_id)
{
	$time = time();
	
	$sql = reset_movie_review_reply_count_sql();
	$result = DBFunction::getInstance()->execute($sql, array($review_id, $time, $review_id));

	return ($result !== false) ? true : false;
}

function delete_movie_review_reply($ids)
{
	$time = time();
	
	$sql = get_review_ids_from_reply($ids);
	$review_ids = DBFunction::getInstance()->getAll($sql);
	
	$sql = delete_movie_review_reply_sql($ids);
	$result = DBFunction::getInstance()->execute($sql);

	if(check_array($review_ids))
	{
		foreach($review_ids as $item)
		{
			reset_movie_review_reply_count($item['movie_review_id']);
		}
	}
	
	return ($result !== false) ? true : false;
}

function add_movie_image($request)
{
	$time = time();
	
	$params = array(
				$request['movie_id'],
				$request['name'],
				'',
				$request['name'],
				$request['size'],
				$request['width'],
				$request['height'],
				$request['category_id'],
				$time);
	
	$sql = add_movie_image_sql();
	
	$results = DBFunction::getInstance()->insert($sql, $params);
	
	return ($results !== false) ? $results : false;
}

function delete_movie_image($request)
{
	$params = array();
	$where = '';
	if(isset($request['id']))
	{
		$where = '`id` = ?';
		$params = array($request['id']);
	}
	else if(isset($request['ids']))
	{
		$where = "`id` in ({$request['ids']})";
	}
	else if(isset($request['movie_id']))
	{
		$where = "`movie_id` = ?";
		$params = array($request['movie_id']);
	}
	
	$sql = delete_movie_image_sql();
	$sql .= $where;
	
	$results = DBFunction::getInstance()->execute($sql, $params);
	
	return ($results !== false) ? $results : false;
}

function get_movie_refer($refer_id)
{
	$key = md5("get_movie_rerefer_$refer_id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_refer_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($refer_id));

	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT);
		return $results;
	}
	else
	{
		return array();
	}
}

function add_movie_refer($request)
{
	$time = time();
	
	$params = array(
				$request['movie_id'],
				$request['refer'],
				$time,
				$time);
	
	$sql = add_movie_refer_sql();
	
	$results = DBFunction::getInstance()->insert($sql, $params);
	
	return ($results !== false) ? $results : false;
}

function update_movie_refer($request)
{
	$time = time();
	
	$params = array(
				$request['movie_id'],
				$request['refer'],
				$time,
				$request['id']);
	
	$sql = update_movie_refer_sql();
	
	$results = DBFunction::getInstance()->execute($sql, $params);
	
	return ($results !== false) ? $results : false;
}

function delete_movie_refer($request)
{
	$params = array();
	$where = '';
	if(isset($request['id']))
	{
		$where = '`id` = ?';
		$params = array($request['id']);
	}
	else if(isset($request['ids']))
	{
		$where = "`id` in ({$request['ids']})";
	}
	else if(isset($request['movie_id']))
	{
		$where = "`movie_id` = ?";
		$params = array($request['movie_id']);
	}
	
	$sql = delete_movie_refer_sql();
	$sql .= $where;
	
	$results = DBFunction::getInstance()->execute($sql, $params);
	
	return ($results !== false) ? $results : false;	
}

function add_movie_titbit($request)
{
	$time = time();
	
	$params = array(
				$request['movie_id'],
				$request['titbit_url'],
				$time);
	
	$sql = add_movie_titbit_sql();
	
	$results = DBFunction::getInstance()->insert($sql, $params);
	
	return ($results !== false) ? $results : false;
}

function get_movie_titbit($id)
{
	$key = md5("get_movie_titbit_$id");
	
	$results = cache_get($key);
	if($results !== false)
	{
		return $results;
	}
	
	$sql = get_movie_titbit_sql();
	$results = DBFunction::getInstance()->getRow($sql, array($id));

	if(check_array($results))
	{
		cache_set($key, $results, CACHE_TIMEOUT);
		return $results;
	}
	else
	{
		return array();
	}
}

function update_movie_titbit($request)
{
	$time = time();
	
	$params = array(
				$request['movie_id'],
				$request['titbit_url'],
				$request['id']);
	
	$sql = update_movie_titbit_sql();
	
	$results = DBFunction::getInstance()->execute($sql, $params);
	
	return ($results !== false) ? $results : false;
}

function delete_movie_titbit($request)
{
	$params = array();
	$where = '';
	if(isset($request['id']))
	{
		$where = '`id` = ?';
		$params = array($request['id']);
	}
	else if(isset($request['ids']))
	{
		$where = "`id` in ({$request['ids']})";
	}
	else if(isset($request['movie_id']))
	{
		$where = "`movie_id` = ?";
		$params = array($request['movie_id']);
	}
	
	$sql = delete_movie_titbit_sql();
	$sql .= $where;
	
	$results = DBFunction::getInstance()->execute($sql, $params);
	
	return ($results !== false) ? $results : false;	
}
